package com.bizfx.component.presenters
{
	import com.bizfx.component.views.INavigator;
	import com.bizfx.component.views.INavigatorDataLoader;
	import com.bizfx.component.views.IView;
	import com.bizfx.component.views.NavigatorItemBase;
	import com.bizfx.events.NavigatorDataLoadedEvent;
	import com.bizfx.events.NavigatorSelectionChangedEvent;
	import com.bizfx.events.ViewEvent;
	
	public class BaseNavigatorPresenter extends BasePresenter
	{
		private var dataLoaded:Boolean = false;
		
		private var _navigatorDataLoader:INavigatorDataLoader;
		
		/**
		 * 导航器数据读取器
		 */
		public function get navigatorDataLoader():INavigatorDataLoader
		{
			return _navigatorDataLoader;
		}
		
		public function set navigatorDataLoader(value:INavigatorDataLoader):void
		{
			if (_navigatorDataLoader != value)
			{
				_navigatorDataLoader = value;
				_navigatorDataLoader.addEventListener(NavigatorDataLoadedEvent.NavigatorData_Loaded, navigatorDataLoadedHandler);
			}
		}
		
		override public function set view(value:IView):void {
			if (value == super.view) {
				return;
			}
			if (this.view) {
				this.view.removeEventListener(ViewEvent.View_Loaded, viewLoadedHandler);
				this.view.removeEventListener(ViewEvent.View_Refresh, viewRefreshHandler);
				this.view.removeEventListener(NavigatorSelectionChangedEvent.Selection_Changed, viewSelectionChangedHandler);
			}
			super.view = value;
			if (this.view) {
				this.view.addEventListener(ViewEvent.View_Loaded, viewLoadedHandler);
				this.view.addEventListener(ViewEvent.View_Refresh, viewRefreshHandler);
				this.view.addEventListener(NavigatorSelectionChangedEvent.Selection_Changed, viewSelectionChangedHandler);
			}
		}
		
		public function BaseNavigatorPresenter()
		{
			super();
		}
		
		protected function navigatorDataLoadedHandler(event:NavigatorDataLoadedEvent):void
		{
			this.dataLoaded = true;
			logger.debug("已经获取导航数据");
			var parent:NavigatorItemBase = INavigator(this.view).getItemByKey(event.parentKey);
			if (parent != null)
			{
				parent.hasLoadedChildren = true;
				INavigator(this.view).clearChildren(event.parentKey);
			}
			
			for (var i:int; i < event.datas.length; i++)
			{
				var item:NavigatorItemBase = event.datas.getItemAt(i) as NavigatorItemBase;
				if (item != null)
				{
					INavigator(this.view).addItem(item);
				}
			}
			
		}
		
		protected function viewSelectionChangedHandler(event:NavigatorSelectionChangedEvent):void
		{
			this.logger.debug("导航器选择项改变了。");
			if (!event.newItem.isLeaf && !event.newItem.hasLoadedChildren)
			{
				this.logger.debug("准备读取导航栏【" + event.newItem.text + "】的子节点数据。");
				this.navigatorDataLoader.LoadData(event.newItem.key);
			}
		}
		
		public function viewLoadedHandler(event:ViewEvent):void
		{
			if (this.dataLoaded) {
				return;
			}
			if (this.navigatorDataLoader != null)
			{
				this.logger.debug("准备读取导航栏数据。");
				this.navigatorDataLoader.LoadData(null);
			}
			else
			{
				this.logger.warn("navigatorDataLoader为空。")
			}
		}
		
		public function viewRefreshHandler(event:ViewEvent):void
		{
			if (this.navigatorDataLoader != null)
			{
				this.logger.debug("准备读取导航栏数据。");
				this.navigatorDataLoader.LoadData(null);
			}
			else
			{
				this.logger.warn("navigatorDataLoader为空。")
			}
		}
	}
}